package com.example.wechat.wechat;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //fragment
    private Fragment fragment_WeChat;
    private Fragment fragment_Contact;
    private Fragment fragment_Discover;
    private Fragment fragment_ME;

    //底端菜单栏LinearLayout
    private LinearLayout linear_WeChat;
    private LinearLayout linear_Contact;
    private LinearLayout linear_Discover;
    private LinearLayout linear_ME;

    //底端菜单栏Imageview
    private ImageView imageView_WeChat;
    private ImageView imageView_Contact;
    private ImageView imageView_Discover;
    private ImageView imageView_ME;

    //底端菜单栏textView
    private TextView textView_WeChat;
    private TextView textView_Contact;
    private TextView textView_Discover;
    private TextView textView_ME;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        InitView();

        InitEvent();

        initFragment(1);

        imageView_WeChat.setImageResource(R.drawable.wechat_pressed);
        textView_WeChat.setTextColor(getResources().getColor(R.color.colorTextViewPress));
    }

    @Override
    public void onClick(View v) {
        //每次点击之后，将所有的ImageView和TextView设置为未选中
        restartButton();
        //再根据所选，进行跳转页面，并将ImageView和TextView设置为选中
        switch (v.getId()){
            case R.id.line_WeChat:
                imageView_WeChat.setImageResource(R.drawable.wechat_pressed);
                textView_WeChat.setTextColor(getResources().getColor(R.color.colorTextViewPress));
                initFragment(1);
                break;

            case R.id.line_Contact:
                imageView_Contact.setImageResource(R.drawable.contact_pressed);
                textView_Contact.setTextColor(getResources().getColor(R.color.colorTextViewPress));
                initFragment(2);
                break;

            case R.id.line_Discover:
                imageView_Discover.setImageResource(R.drawable.discover_pressed);
                textView_Discover.setTextColor(getResources().getColor(R.color.colorTextViewPress));
                initFragment(3);
                break;

            case R.id.line_ME:
                imageView_ME.setImageResource(R.drawable.me_pressed);
                textView_ME.setTextColor(getResources().getColor(R.color.colorTextViewPress));
                initFragment(4);
                break;
        }

    }


    private void InitView(){
        linear_WeChat = findViewById(R.id.line_WeChat);
        linear_Contact = findViewById(R.id.line_Contact);
        linear_Discover = findViewById(R.id.line_Discover);
        linear_ME = findViewById(R.id.line_ME);

        imageView_WeChat = findViewById(R.id.imageWeChat);
        imageView_Contact  = findViewById(R.id.imageContact);
        imageView_Discover = findViewById(R.id.imageDiscover);
        imageView_ME = findViewById(R.id.imageMe);

        textView_WeChat = findViewById(R.id.WeChat);
        textView_Contact = findViewById(R.id.Contact);
        textView_Discover = findViewById(R.id.Discover);
         textView_ME  = findViewById(R.id.Me);

    }

    private void initFragment(int index)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //将所有的Fragment隐藏
        hideAllFragment(fragmentTransaction);
        switch (index)
        {
            case 1:
                if(fragment_WeChat == null)
                {
                    fragment_WeChat = new ContentFragment_WeChat();
                    fragmentTransaction.add(R.id.frame_content,fragment_WeChat);
                }
                 else
                    fragmentTransaction.show(fragment_WeChat);
                break;

            case 2:
                if(fragment_Contact == null)
                {
                    fragment_Contact = new ContentFragment_Contact();
                    fragmentTransaction.add(R.id.frame_content,fragment_Contact);
                }
                else
                    fragmentTransaction.show(fragment_Contact);
                break;

            case 3:
                if(fragment_Discover == null)
                {
                    fragment_Discover = new ContentFragment_Discover();
                    fragmentTransaction.add(R.id.frame_content,fragment_Discover);
                }
                else
                    fragmentTransaction.show(fragment_Discover);
                break;

            case 4:
                if(fragment_ME == null)
                {
                    fragment_ME = new ContentFragment_ME();
                    fragmentTransaction.add(R.id.frame_content,fragment_ME);
                }
                else
                    fragmentTransaction.show(fragment_ME);
                break;
        }
        //提交事务
        fragmentTransaction.commit();
    }

    private void InitEvent(){
        //设置LinearLayout监听
        linear_WeChat.setOnClickListener(this);
        linear_Contact.setOnClickListener(this);
        linear_Discover.setOnClickListener(this);
        linear_ME.setOnClickListener(this);
    }


    //隐藏所有的Fragment
    private void hideAllFragment(FragmentTransaction transaction){
        if (fragment_WeChat!= null){
            transaction.hide(fragment_WeChat);
        }

        if (fragment_Contact!= null){
            transaction.hide(fragment_Contact);
        }

        if (fragment_Discover!= null){
            transaction.hide(fragment_Discover);
        }

        if(fragment_ME != null){
            transaction.hide(fragment_ME);
        }

        // transaction.commit();
    }


    //重新设置ImageView和TextView的状态
    private void restartButton(){
        //设置为未点击状态
        imageView_WeChat.setImageResource(R.drawable.wechat_normal);
        imageView_Contact.setImageResource(R.drawable.contact_normal);
        imageView_Discover.setImageResource(R.drawable.discover_normal);
        imageView_ME.setImageResource(R.drawable.me_normal);

        //设置为灰色
       textView_WeChat.setTextColor(getResources().getColor(R.color.colorTextViewNormal));
        textView_Contact.setTextColor(getResources().getColor(R.color.colorTextViewNormal));
       textView_Discover.setTextColor(getResources().getColor(R.color.colorTextViewNormal));
       textView_ME.setTextColor(getResources().getColor(R.color.colorTextViewNormal));
    }

}
